import React, { Component } from 'react'
import Header from '../../components/Header/Header'
import { Button, Toast } from 'antd-mobile'
import { reqgetgoodsinfo,reqcartadd } from '../../http/api'
import { $pre } from '../../http/http';
import { Link } from 'react-router-dom'
import './Detail.less'
export default class Detail extends Component {
  constructor() {
    super();
    this.state = {
      info: {},
    };
  }
  async componentDidMount() {
    let res = await reqgetgoodsinfo({ id: this.props.match.params.id })
    this.setState({
      info: res.data.list[0]
    })
  }
  async addShop(){
    // if(!type){type=1}
    let res=await reqcartadd({
      uid:JSON.parse(sessionStorage.getItem('userInfo')).uid,
      type:1,
      // type,
      goodsid:this.props.match.params.id,
      num:1,
    })
    if(res.data.code==200){
      Toast.show({
        content:res.data.msg
      })
    }
  }
  render() {
    let { info } = this.state;
    return (
      <div>
        <Header title='商品详情' back></Header>
        <div className='card' >
          <img src={$pre + info.img} alt="" />
          <div >
            <h3>商品名称:{info.goodsname}</h3>
            <div>价格:{info.price}</div>
            <div>市场价格：{info.market_price}</div>
          </div>
        </div>
        <Button color='primary' onClick={()=>this.addShop()}>加入购物车</Button>
        <Link to={"/index/shop"} className="button">跳转购物车</Link>
      </div>
    )
  }
}
